<template>
  <div class="app-container">
    <el-card class="content-edit">
      <div slot="header">{{ pageType==='add'?'添加路线':'编辑路线' }}</div>
      <el-form ref="form" :model="form" label-width="120px">
        <el-form-item label="线路名称" maxlength="20">
          <el-input v-model="form['name']"/>
        </el-form-item>
        <el-form-item label="接送类型">
          <el-radio-group v-model="form['type']">
            <el-radio-button :label="1">接机</el-radio-button>
            <el-radio-button :label="2">送机</el-radio-button>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="出发地">
          <el-select v-if="form['type']===1" v-model="form['airport']" placeholder="请选择机场">
            <el-option v-for="item in airportList" :key="item.id" :label="item.name" :value="item.id"/>
          </el-select>
          <el-select v-if="form['type']!==1" v-model="form['provinceCodeIdx']" placeholder="请选择省份"
                     @change="provinceSelectChange">
            <el-option v-for="(item,index) in provinceList" :key="item.code" :label="item.name" :value="index"/>
          </el-select>
          <el-select v-if="form['type']!==1" v-model="form['cityCodeIdx']" placeholder="请选择市"
                     @change="citySelectChange">
            <el-option v-for="(item,index) in cityList" :key="item.code" :label="item.name" :value="index"/>
          </el-select>
          <el-select v-if="form['type']!==1 && areaList.length!=0" v-model="form['areaCode']" placeholder="请选择区">
            <el-option v-for="item in areaList" :key="item.code" :label="item.name" :value="item.code"/>
          </el-select>
        </el-form-item>
        <el-form-item label="目的地">
          <el-select v-if="form['type']!==1" v-model="form['airport']" placeholder="请选择机场">
            <el-option v-for="item in airportList" :key="item.id" :label="item.name" :value="item.id"/>
          </el-select>
          <el-select v-if="form['type']===1" v-model="form['provinceCodeIdx']" placeholder="请选择省份"
                     @change="provinceSelectChange">
            <el-option v-for="(item,index) in provinceList" :key="item.code" :label="item.name" :value="index"/>
          </el-select>
          <el-select v-if="form['type']===1" v-model="form['cityCodeIdx']" placeholder="请选择市"
                     @change="citySelectChange">
            <el-option v-for="(item,index) in cityList" :key="item.code" :label="item.name" :value="index"/>
          </el-select>
          <el-select v-if="form['type']===1 && areaList.length!=0" v-model="form['areaCode']" placeholder="请选择区">
            <el-option v-for="item in areaList" :key="item.code" :label="item.name" :value="item.code"/>
          </el-select>
        </el-form-item>
        <el-form-item label="车型选择">
          <el-select v-model="form['carType']" placeholder="车辆类型">
            <el-option v-for="item in carTypeList" :key="item.id" :value="item['id']" :label="item['name']"/>
          </el-select>
        </el-form-item>
        <el-form-item label="收费方式">
          <el-radio-group v-model="form['chargeType']">
            <el-radio :label="0">按人数</el-radio>
            <el-radio :label="1">按车辆</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="车票价格" class="ticket">
          <div class="item">
            <span @click.stop.prevent="radioClick('openDefault')">
              <el-radio v-model="form['openDefault']" :label="1">&nbsp;</el-radio>
            </span>
            <span>默认</span>
            <el-input v-model="form['defaultPrice']" type="number" class="ticket-input">
              <span slot="prepend">车票</span>
              <span slot="suffix" class="span">元</span>
            </el-input>
            <el-input v-model="form['defaultCommission']" type="number" class="ticket-input">
              <span slot="prepend">佣金</span>
              <span slot="suffix" class="span">元</span>
            </el-input>
          </div>
          <div class="item">
            <span @click.stop.prevent="radioClick('openWeekend')">
              <el-radio v-model="form['openWeekend']" :label="1">&nbsp;</el-radio>
            </span>
            <span>周末</span>
            <el-input v-model="form['weekendPrice']" type="number" class="ticket-input">
              <span slot="prepend">车票</span>
              <span slot="suffix" class="span">元</span>
            </el-input>
            <el-input v-model="form['weekendCommission']" type="number" class="ticket-input">
              <span slot="prepend">佣金</span>
              <span slot="suffix" class="span">元</span>
            </el-input>
          </div>
          <div class="item">
            <span @click.stop.prevent="radioClick('openDate')">
              <el-radio v-model="form['openDate']" :label="1">&nbsp;</el-radio>
            </span>
            <span>指定日期</span>
            <el-date-picker
              v-model="form['dateBetween']"
              class="margin-left-20"
              type="daterange"
              range-separator="-"
              start-placeholder="开始日期"
              end-placeholder="结束日期"
              value-format="yyyy-MM-dd"/>
            <div style="margin: 10px 0 0 44px">
              <el-input v-model="form['datePrice']" type="number" class="ticket-input">
                <span slot="prepend">车票</span>
                <span slot="suffix" class="span">元</span>
              </el-input>
              <el-input v-model="form['dateCommission']" type="number" class="ticket-input">
                <span slot="prepend">佣金</span>
                <span slot="suffix" class="span">元</span>
              </el-input>
            </div>
          </div>
        </el-form-item>
        <!--<el-form-item label="荷载人数">
          <el-input v-model="form['rideNumber']" type="number" style="width: 140px"/>
        </el-form-item>-->
        <el-form-item>
          <el-button :loading="loading" type="primary" @click="onSubmit">保存</el-button>
          <el-button @click="$router.back()">取消</el-button>
        </el-form-item>
      </el-form>
    </el-card>
  </div>
</template>

<script>
  export default {
    name: 'RouteAirportModify',
    data() {
      return {
        pageType: 'add',
        loading: false,
        // 机场列表
        airportList: [],
        // 车辆类型列表
        carTypeList: [],
        // 省市区列表
        provinceList: [],
        // 市列表
        cityList: [],
        // 地区列表
        areaList: [],

        form: {
          // 路线名称
          name: '',
          // 类型
          type: 1,
          // 车辆类型
          carType: '',
          // 所选机场
          airport: '',
          // 省
          provinceCodeIdx: '',
          provinceCode: '',
          // 市
          cityCodeIdx: '',
          cityCode: '',
          // 区
          areaCode: '',
          // 收费方式
          chargeType: 0,
          // 选择默认价格
          openDefault: 0,
          // 选择周末价格
          openWeekend: 0,
          // 选择特殊日期价格
          openDate: 0,
          // 成人默认价格
          defaultPrice: '',
          // 成人默认佣金
          defaultCommission: '',
          // 成人周末价格
          weekendPrice: '',
          // 成人周末佣金
          weekendCommission: '',
          // 成人指定日期价格
          datePrice: '',
          // 成人指定日期佣金
          dateCommission: '',
          // 指定日期
          dateBetween: []
        }
      }
    },
    mounted(){
      this.form['id'] = this.$router.currentRoute.query['id'] || '';
      if (this.$router.currentRoute.path.indexOf('add') > 0) {
        this.pageType = 'add'
      } else {
        this.pageType = 'edit'
      }
      this.loadCarTypeList();
    },
    methods: {
      // 加载车辆类型列表
      loadCarTypeList(){
        (async () => {
          const result = await this.$service.applicationGetAllCarType();
          if (result.code === 0) {
            this.carTypeList = result.data;
            this.loadAirportList();
          }
        })();
      },
      // 加载机场列表
      loadAirportList(){
        (async () => {
          const result = await this.$service.applicationGetAllAirport();
          if (result.code === 0) {
            this.airportList = result.data;
            this.loadProvinceList();
          }
        })();
      },
      // 加载省市区列表
      loadProvinceList(){
        (async () => {
          const result = await this.$service.applicationGetProvinceList();
          if (result.code === 0) {
            this.provinceList = result.data;
            if (this.pageType === 'edit') {
              this.loadData();
            }
          }
        })();
      },
      // 加载路线信息
      loadData(){
        (async () => {
          const result = await this.$service.applicationGetAirportRouteDetails({id: this.form['id']});
          if (result.code === 0) {
            const data = result.data || {};
            for (let i = 0; i < this.provinceList.length; i++) {
              if (this.provinceList[i].code === data['provinceCode']) {
                data['provinceCodeIdx'] = i;
                this.cityList = this.provinceList[i]['children'];
                for (let j = 0; j < this.provinceList[i]['children'].length; j++) {
                  if (this.provinceList[i]['children'][j].code === data['cityCode']) {
                    data['cityCodeIdx'] = j;
                    this.areaList = this.provinceList[i]['children'][j]['children'] || [];
                  }
                }
              }
            }
            data['dateBetween'] && (data['dateBetween'] = data['dateBetween'].split(','));
            Object.assign(this.form, data);
          }
        })();
      },
      // 省份选择监听e:选择的index,params:出发地还是终点
      provinceSelectChange(e){
        // console.log(e);
        this['cityList'] = this.provinceList[e]['children'];
        this.form['provinceCode'] = this.provinceList[e]['code'];
        this.form['cityCodeIdx'] = '';
        this.form['cityCode'] = '';
        this.form['areaCode'] = '';
      },
      // 市级选择监听e:选择的index,params:出发地还是终点
      citySelectChange(e){
        // console.log(e);
        this['areaList'] = this['cityList'][e]['children'] || [];
        this.form['cityCode'] = this['cityList'][e]['code'];
        this.form['areaCode'] = '';
      },
      // 车票选择监听
      radioClick(params){
        this.form[params] = this.form[params] ? 0 : 1
      },
      // 提交
      onSubmit() {
        this.$refs.form.validate(valid => {
          if (valid) {
            this.loading = true;
            (async () => {
              const params = Object.assign({}, this.form);
              // console.log(params);
              let result = null;
              if (this.pageType === 'add') {
                result = await this.$service.applicationAddAirportRoute(params);
              } else {
                result = await this.$service.applicationModifyAirportRoute(params);
              }
              this.loading = false;
              if (result.code === 0) {
                this.$message.success('操作成功~');
                this.$router.back();
              } else {
                this.$message.error(result.msg);
              }
            })();
          } else {
            console.log('error submit!!');
            return false;
          }
        });
      }
    }
  }
</script>

<style lang="scss" type="text/scss" scoped>
  @import '../../../styles/index.scss';
  .ticket{
    .item{
      margin-bottom: 10px;
    }
    span{
      font-weight: 500;
      margin-right: 10px;
      line-height: 38px;
    }
    .el-input{
      margin-top: 1px;
      width: 200px;
      margin-left: 20px;
      .span{
        margin-right: 10px;
        line-height: 40px;
      }
    }
  }
</style>
